package com.masterclient.app.businessmodule.vipmodule.card;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.vipmodule.bean.Order;
import com.masterclient.app.businessmodule.vipmodule.persenter.CardBtnContract;
import com.masterclient.app.businessmodule.vipmodule.persenter.CardBtnPresenter;
import com.masterclient.app.businessmodule.vipmodule.view.OrderView;
import com.masterclient.app.utils.CommonUtil;
import com.masterclient.app.widget.CommonDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;


/**
 * 零售订单-代发货
 */
public class OrderDfhCard extends FCBaseCard<CardBtnPresenter, Order> implements CardBtnContract.View {
    @BindView(R.id.orderViwe)
    OrderView orderViwe;
    @BindView(R.id.btn_tv)
    TextView btnTv;
    @BindView(R.id.des_tv)
    TextView desTv;
    private Order.ButtonBean buttonBean;
    private String id;
    public OrderDfhCard(View itemView, Context context) {
        super(itemView, context);
    }

    @Override
    public void onBindViewHolder(Order order, int i) {
        orderViwe.setOrder(order);
        id = order.getOrder().getId();
        buttonBean = order.getButton();
        int replenish_btn = buttonBean.getReplenish_btn();// 补货
        int delivery_btn = buttonBean.getDelivery_btn();// 发货
        if (replenish_btn == 0 && delivery_btn == 0) {
            btnTv.setVisibility(View.GONE);
            desTv.setVisibility(View.VISIBLE);
            desTv.setText("已确认发货，等待公司发货");
        } else if (replenish_btn == 1) {
            desTv.setVisibility(View.VISIBLE);
            desTv.setText("库存不足");
            btnTv.setVisibility(View.VISIBLE);
            btnTv.setText("立即补货");
            btnTv.setTextColor(mContext.getResources().getColor(R.color.white));
            btnTv.setBackgroundColor(mContext.getResources().getColor(R.color.color_212121));
        } else if (delivery_btn == 1) {
            desTv.setVisibility(View.GONE);
            btnTv.setVisibility(View.VISIBLE);
            btnTv.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            btnTv.setText("立即发货");
            btnTv.setBackgroundResource(R.drawable.withbrawal_bg);
        }
        btnTv.setOnClickListener(v -> {

            if (replenish_btn == 1)// 立即补货
                doReplenishBtn();
            else if (delivery_btn == 1)// 立即发货
                doDeliveryBtn(order.getOrder().getId());
        });
    }

    /**
     * 立即发货
     */
    private void doDeliveryBtn(String orderId) {
        baseShowCommitLoading("");
        mPresenter.sellerOrderPush(orderId);
    }

    /**
     * 立即补货
     */
    private void doReplenishBtn() {
        showCommonDialog("请前往葵花子APP补货","确定","取消");
    }

    @Override
    public void succ(CommonBaseResponse bean) {
        ToastUtil.showToast(bean.getMsg());
        EventBus.getDefault().post(new Order());
    }

    /**
     * 显示dialog
     */
    private void showCommonDialog(String title, String confirmTxt, String cancelTxt) {
        new CommonDialog.Builder(mContext)
                .setTitle(title)
                .setOnConfirmClickListener(confirmTxt, () -> CommonUtil.openUrl(mContext,
                        "xrkb://com.flowerbusiness.app/cart?order_id="+id))
                .setOnCancelClickListener(cancelTxt, null)
                .createDialog().show();
    }
}
